﻿using SimS7Study.Lib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SimS7Study
{
    /// <summary>
    /// CaseSelf.xaml 的交互逻辑
    /// </summary>
    public partial class CaseSelf : Window, INotifyPropertyChanged
    {
        S7Lib s7Lib;
        public CaseSelf()
        {
            InitializeComponent();
            DataContext = this; // 数据上下文

            #region 读Q区
            //Task.Run(async () =>
            //{
            //    while (true)
            //    {
            //        // 这里虽然没有绑定到UI线程，但是用到属性设置有时候会出问题，所以要Invoke
            //        Dispatcher.Invoke(() =>
            //        {
            //            ReadQ00();
            //            ReadQ01();
            //            ReadQ();
            //        });

            //        await Task.Delay(1000);
            //    }
            //});
            #endregion

            #region 读DB区
            //Task.Run(async () =>
            //{
            //    while (true)
            //    {
            //        // 这里虽然没有绑定到UI线程，但是用到属性设置有时候会出问题，所以要Invoke
            //        Dispatcher.Invoke(() =>
            //        {
            //            //ReadWord0();
            //            //ReadWord2();
            //            //ReadReal4();
            //            //ReadStr8();
            //            ReadAllDB();
            //        });

            //        await Task.Delay(1000);
            //    }
            //});
            #endregion

            #region 自己封装的批量读：一次性读取Q区和DB区
            Task.Run(async () =>
            {
                while (true)
                {
                    Dispatcher.Invoke(() =>
                    {
                        ReadQAndDB();
                    });

                    await Task.Delay(1000);
                }
            });
            #endregion

        }

        #region 通知
        public event PropertyChangedEventHandler? PropertyChanged;

        /// <summary>
        /// 通知
        /// </summary>
        /// <param name="propName"></param>
        private void NotifyChanged(string propName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
        #endregion

        #region 绑定属性

        private bool q00;

        public bool Q00
        {
            get { return q00; }
            set
            {
                q00 = value;
                NotifyChanged("Q00"); // 属性发生改变发起通知
            }
        }

        private bool q01;

        public bool Q01
        {
            get { return q01; }
            set
            {
                q01 = value;
                NotifyChanged("Q01"); // 属性发生改变发起通知
            }
        }

        private bool q02;

        public bool Q02
        {
            get { return q02; }
            set
            {
                q02 = value;
                NotifyChanged("Q02"); // 属性发生改变发起通知
            }
        }

        private bool q03;

        public bool Q03
        {
            get { return q03; }
            set
            {
                q03 = value;
                NotifyChanged("Q03");
            }
        }

        private bool q04;

        public bool Q04
        {
            get { return q04; }
            set
            {
                q04 = value;
                NotifyChanged("Q04");
            }
        }

        private bool q05;

        public bool Q05
        {
            get { return q05; }
            set
            {
                q05 = value;
                NotifyChanged("Q05");
            }
        }

        private bool q06;

        public bool Q06
        {
            get { return q06; }
            set
            {
                q06 = value;
                NotifyChanged("Q06");
            }
        }

        private bool q07;

        public bool Q07
        {
            get { return q07; }
            set
            {
                q07 = value;
                NotifyChanged("Q07");
            }
        }



        #endregion

        #region 连接控制
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                s7Lib = new S7Lib();
                s7Lib.Connect(txtIP.Text, 0, 1);

                btnConn.Visibility = Visibility.Collapsed;//隐藏连接按钮
                btnDisConn.Visibility = Visibility.Visible;//显示断开按钮
            }
            catch (Exception)
            {
                btnConn.Visibility = Visibility.Visible;//显示连接按钮
                btnDisConn.Visibility = Visibility.Collapsed;//隐藏断开按钮            
            }

        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDisConn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (s7Lib != null && s7Lib.Connected)
                {
                    s7Lib.DisConnect();

                    btnConn.Visibility = Visibility.Visible;//显示连接按钮
                    btnDisConn.Visibility = Visibility.Collapsed;//隐藏断开按钮 
                }
            }
            catch (Exception)
            {
                btnConn.Visibility = Visibility.Collapsed;//隐藏连接按钮
                btnDisConn.Visibility = Visibility.Visible;//显示断开按钮     
            }
        }
        #endregion

        #region 读Q区
        /// <summary>
        /// 读Q0.0  第0个字节第0位
        /// </summary>
        private void ReadQ00()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                ReadDataParameter readDataParameter = new ReadDataParameter
                {
                    Area = Area.Q,
                    DBNumber = 0,  // 读的不是数据块，直接设置0
                    ParameterType = ParameterType.BIT, // 读取的参数类型是bit
                    ReadCount = 1, // 读取1个
                    ByteAddress = 0, // 第0个字节
                    BitAddress = 0,  // 第0位
                };

                byte[] result = s7Lib.ReadSingle(readDataParameter);

                Q00 = (result[0] == 0x01);
            }
        }

        /// <summary>
        /// 读Q0.1  第0个字节第1位
        /// </summary>
        private void ReadQ01()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                ReadDataParameter readDataParameter = new ReadDataParameter
                {
                    Area = Area.Q,
                    DBNumber = 0,  // 读的不是数据块，直接设置0
                    ParameterType = ParameterType.BIT, // 读取的参数类型是bit
                    ReadCount = 1, // 读取1个
                    ByteAddress = 0, // 第0个字节
                    BitAddress = 1,  // 第0位
                };

                byte[] result = s7Lib.ReadSingle(readDataParameter);

                Q01 = (result[0] == 0x01);
            }
        }

        /// <summary>
        /// 读取字节  读Q0.0~Q0.7
        /// </summary>
        private void ReadQ()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                ReadDataParameter readDataParameter = new ReadDataParameter
                {
                    Area = Area.Q,
                    DBNumber = 0,  // 读的不是数据块，直接设置0
                    ParameterType = ParameterType.BYTE, // 读取的参数类型是byte
                    ReadCount = 1, // 读取1个
                    ByteAddress = 0, // 第0个字节
                    BitAddress = 0,  // 第0位
                };

                byte[] result = s7Lib.ReadSingle(readDataParameter);

                // 将byte转成bool类型的数组
                BitArray bitArray = new BitArray(result);
                bool[] boolArray = new bool[8];
                bitArray.CopyTo(boolArray, 0);

                Q00 = boolArray[0];
                Q01 = boolArray[1];
                Q02 = boolArray[2];
                Q03 = boolArray[3];
                Q04 = boolArray[4];
                Q05 = boolArray[5];
                Q06 = boolArray[6];
                Q07 = boolArray[7];
            }
        }

        #endregion

        #region 读DB区


        // PLC的word->ushort PLC的int->short PLC的real->float  PLC的string[10]->string


        private void ReadWord0()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                // 读取db1的第0个字节读取1个word (或者可以从第0个字节开始读2个字节)
                ReadDataParameter dataParameter = new ReadDataParameter
                {
                    Area = Area.DBAndV,
                    DBNumber = 1,
                    ParameterType = ParameterType.WORD,
                    ByteAddress = 0,
                    BitAddress = 0,
                    ReadCount = 1,
                };

                byte[] resultBytes = s7Lib.ReadSingle(dataParameter);

                ushort us = BitConverter.ToUInt16([resultBytes[1], resultBytes[0]]);
                txtDBW0.Text = us.ToString();
            }
        }

        private void ReadWord2()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                // 读取db1的第2个字节读取1个int (或者可以从第2个字节开始读2个字节)
                ReadDataParameter dataParameter = new ReadDataParameter
                {
                    Area = Area.DBAndV,
                    DBNumber = 1,
                    ParameterType = ParameterType.INT, // 这里的int是PLC的int   PLC的int->short
                    ByteAddress = 2,
                    BitAddress = 0,
                    ReadCount = 1,
                };

                byte[] resultBytes = s7Lib.ReadSingle(dataParameter);

                short us = BitConverter.ToInt16([resultBytes[1], resultBytes[0]]);
                txtDBW2.Text = us.ToString();
            }
        }

        private void ReadReal4()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                // 读取db1的第4个字节读取1个Real (或者可以从第4个字节开始读4个字节)
                ReadDataParameter dataParameter = new ReadDataParameter
                {
                    Area = Area.DBAndV,
                    DBNumber = 1,
                    ParameterType = ParameterType.REAL, //   PLC的Real->float
                    ByteAddress = 4,
                    BitAddress = 0,
                    ReadCount = 1,
                };

                //ReadDataParameter dataParameter = new ReadDataParameter
                //{
                //    Area = Area.DBAndV,
                //    DBNumber = 1,
                //    ParameterType = ParameterType.BYTE, 
                //    ByteAddress = 4,
                //    BitAddress = 0,
                //    ReadCount = 4,  // 类型为Byte要读取4个字节
                //};

                byte[] resultBytes = s7Lib.ReadSingle(dataParameter);

                float us = BitConverter.ToSingle(resultBytes.Reverse().ToArray());
                //float us = BitConverter.ToSingle([resultBytes[3],resultBytes[2],resultBytes[1],resultBytes[0]]);
                txtDBD4.Text = us.ToString();
            }
        }

        /// <summary>
        /// 读字符串
        /// </summary>
        private void ReadStr8()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                if (s7Lib != null && s7Lib.Connected)
                {
                    // 读取db1从第8个字节开始读12个字节)
                    ReadDataParameter dataParameter = new ReadDataParameter
                    {
                        Area = Area.DBAndV,
                        DBNumber = 1,
                        ParameterType = ParameterType.BYTE,
                        ByteAddress = 8,
                        BitAddress = 0,
                        ReadCount = 12,  // PLC中是string[10],但是要读取12个字节,第0个字节表示字符串总长度,第1个字节表示的是字符串实际长度
                    };


                    byte[] resultBytes = s7Lib.ReadSingle(dataParameter);

                    //resultBytes[1]:表示字符串的实际长度
                    byte[] strBytes = resultBytes.ToList().GetRange(2, resultBytes[1]).ToArray();
                    string str = Encoding.ASCII.GetString(strBytes);
                    txtStr8.Text = str.ToString();
                }
            }
        }

        /// <summary>
        ///  批量读取(连续的字节)
        ///  一次连续读取20个字节 word->ushort(DB1.DBW0)  PLC的int->short(DB1.DBW2)  PLC的real->float(DB1.DBD4)  PLC的string[10]->string(P#DB1.DBX8.0)
        /// </summary>
        private void ReadAllDB()
        {

            if (s7Lib != null && s7Lib.Connected)
            {
                // 读取db1从第0个字节开始读20个byte
                ReadDataParameter dataParameter = new ReadDataParameter
                {
                    Area = Area.DBAndV,
                    DBNumber = 1,
                    ParameterType = ParameterType.BYTE,
                    ByteAddress = 0,
                    BitAddress = 0,
                    ReadCount = 20,
                };


                byte[] resultBytes = s7Lib.ReadSingle(dataParameter);

                //Word DB1.DBW0
                ushort us = BitConverter.ToUInt16([resultBytes[1], resultBytes[0]]);
                txtDBW0.Text = us.ToString();
                // PLC Int-->c# short DB1.DBW2
                short s = BitConverter.ToInt16([resultBytes[3], resultBytes[2]]);
                txtDBW2.Text = s.ToString();
                // PLC real->c# float DB1.DBD4
                float f = BitConverter.ToSingle([resultBytes[7], resultBytes[6], resultBytes[5], resultBytes[4]]);
                txtDBD4.Text = f.ToString();

                //resultBytes[10] 字符串的总长度,resultBytes[9]:表示字符串的实际长度
                byte[] strBytes = resultBytes.ToList().GetRange(10, resultBytes[9]).ToArray();
                string str = Encoding.ASCII.GetString(strBytes);
                txtStr8.Text = str.ToString();
            }

        }
        #endregion

        #region 自己封装的批量读：一次性读取Q区和DB区
        private void ReadQAndDB()
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                // Q0.0 (读取Q区第0个字节)
                ReadDataParameter dataParameter1 = new ReadDataParameter
                {
                    Area = Area.Q,
                    DBNumber = 0,
                    ParameterType = ParameterType.BYTE,
                    ByteAddress = 0,
                    BitAddress = 0,
                    ReadCount = 1,
                };

                // DB1的第0个字节开始读，读20个字节
                ReadDataParameter dataParameter2 = new ReadDataParameter
                {
                    Area = Area.DBAndV,
                    DBNumber = 1,
                    ParameterType = ParameterType.BYTE,
                    ByteAddress = 0,
                    BitAddress = 0,
                    ReadCount = 20,
                };

                s7Lib.ReadMulti(dataParameter1, dataParameter2);


                #region 显示Q区
                // 将byte转成bool类型的数组
                BitArray bitArray = new BitArray(dataParameter1.ResultDataBytes);
                bool[] boolArray = new bool[8];
                bitArray.CopyTo(boolArray, 0);

                Q00 = boolArray[0];
                Q01 = boolArray[1];
                Q02 = boolArray[2];
                Q03 = boolArray[3];
                Q04 = boolArray[4];
                Q05 = boolArray[5];
                Q06 = boolArray[6];
                Q07 = boolArray[7];
                #endregion

                #region 显示DB区
                byte[] resultBytes = dataParameter2.ResultDataBytes;
                //Word DB1.DBW0
                ushort us = BitConverter.ToUInt16([resultBytes[1], resultBytes[0]]);
                txtDBW0.Text = us.ToString();
                // PLC Int-->c# short DB1.DBW2
                short s = BitConverter.ToInt16([resultBytes[3], resultBytes[2]]);
                txtDBW2.Text = s.ToString();
                // PLC real->c# float DB1.DBD4
                float f = BitConverter.ToSingle([resultBytes[7], resultBytes[6], resultBytes[5], resultBytes[4]]);
                txtDBD4.Text = f.ToString();

                //resultBytes[10] 字符串的总长度,resultBytes[9]:表示字符串的实际长度
                byte[] strBytes = resultBytes.ToList().GetRange(10, resultBytes[9]).ToArray();
                string str = Encoding.ASCII.GetString(strBytes);
                txtStr8.Text = str.ToString();
                #endregion


            }
        }
        #endregion

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            #region 写单项  从第0个字节开始写一个word
            //ushort us = 999;
            //byte[] usBytes = BitConverter.GetBytes(us).Reverse().ToArray();

            //WriteDataParameter writeDataParameter = new WriteDataParameter()
            //{
            //    Area = Area.DBAndV,
            //    ParameterType = ParameterType.WORD,
            //    WriteCount = 1,
            //    ByteAddress = 0,
            //    BitAddress = 0,
            //    DataType = DataType.WORD,
            //    DBNumber = 1,
            //    WriteBytes = usBytes
            //};

            //s7Lib.WriteSingle(writeDataParameter);

            #endregion

            #region 写单项  从第2个字节开始写一个PLC int-->c# short
            //short s = 777;
            //byte[] sBytes = BitConverter.GetBytes(s).Reverse().ToArray();

            //WriteDataParameter writeDataParameter = new WriteDataParameter()
            //{
            //    Area = Area.DBAndV,
            //    ParameterType = ParameterType.INT,
            //    WriteCount = 1,
            //    ByteAddress = 2,
            //    BitAddress = 0,
            //    DataType = DataType.INT,
            //    DBNumber = 1,
            //    WriteBytes = sBytes
            //};

            //s7Lib.WriteSingle(writeDataParameter);
            #endregion

            #region 写单项 从第0个字节开始写2个word
            //// PLC word -> c# ushort
            //ushort us = 111;
            //byte[] usBytes = BitConverter.GetBytes(us).Reverse().ToArray();
            //// PLC int --> c# short
            //short s = 222;
            //byte[] sBytes = BitConverter.GetBytes(s).Reverse().ToArray();

            //// 合并的byte数组
            //List<byte> totalList = usBytes.ToList();
            //totalList.AddRange(sBytes);

            //WriteDataParameter writeDataParameter = new WriteDataParameter()
            //{
            //    Area = Area.DBAndV,
            //    ParameterType = ParameterType.WORD,
            //    WriteCount = 2,
            //    ByteAddress = 0,
            //    BitAddress = 0,
            //    DataType = DataType.WORD,
            //    DBNumber = 1,
            //    WriteBytes = totalList.ToArray()
            //};

            //s7Lib.WriteSingle(writeDataParameter);
            #endregion

            #region 写单项  Q0.1
            //WriteDataParameter writeDataParameter = new WriteDataParameter()
            //{
            //    Area = Area.Q,
            //    ParameterType = ParameterType.BIT,
            //    WriteCount = 1,
            //    ByteAddress = 0,
            //    BitAddress = 1,
            //    DataType = DataType.BIT,
            //    DBNumber = 0,
            //    WriteBytes = [0x00]   // 写false,就传0x00
            //};

            //s7Lib.WriteSingle(writeDataParameter);
            #endregion

            #region 写单项 字符串 PLC定义的是String[10] 从第8个字节写字符串写longma

            //byte [] strBytes = Encoding.ASCII.GetBytes("longma");
            //// 字符串前面2个字节一个表示总长度，一个表示实际长度
            //List<byte> totalBytes = [0x0a,(byte)strBytes.Length];
            //totalBytes.AddRange(strBytes);

            //WriteDataParameter writeDataParameter = new WriteDataParameter()
            //{
            //    Area = Area.DBAndV,
            //    ParameterType = ParameterType.BYTE,
            //    WriteCount = (short)totalBytes.Count,   // 这里是实际写的字节个数,string[10],最多能写12个字节
            //    ByteAddress = 8,
            //    BitAddress = 0,
            //    DataType = DataType.BYTE,
            //    DBNumber = 1,
            //    WriteBytes = totalBytes.ToArray()
            //};

            //s7Lib.WriteSingle(writeDataParameter);

            // 将longma666写入到string[10]的字符串中
            //s7Lib.WriteString(1, 0x0a, 8, 0, "longma666");
            #endregion

            #region 写多项测试
            //1、写Q0.0
            //2. DB区 从第0个开始写一个word
            WriteDataParameter writeDataParameter1 = new WriteDataParameter() 
            {
               Area=Area.Q,
               ByteAddress=0,
               BitAddress=0,
               ParameterType= ParameterType.BIT,
               DataType=DataType.BIT,
               DBNumber=0,
               WriteCount=1,
               WriteBytes = [0x01]
            };

            ushort us = 888;
            byte[] usBytes = BitConverter.GetBytes(us).Reverse().ToArray();
            WriteDataParameter writeDataParameter2 = new WriteDataParameter()
            {
                Area = Area.DBAndV,
                ByteAddress = 0,
                BitAddress = 0,
                ParameterType = ParameterType.WORD,
                DataType = DataType.WORD,
                DBNumber = 1,
                WriteCount = 1,
                WriteBytes = usBytes
            };

            s7Lib.WriteMulti(writeDataParameter1, writeDataParameter2);

            #endregion
        }

        #region 写Q区
        /// <summary>
        /// 写Q0.0
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ00_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 0,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q00 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);

                //s7Lib.WriteBool(Area.Q, 0, 0, Q00);
            }
        }

        /// <summary>
        /// 写Q0.1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void cbQ01_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 1,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q01 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);

                //s7Lib.WriteBool(Area.Q, 0, 1, Q00);
            }
        }


        /// <summary>
        /// 写Q0.2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ02_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 2,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q02 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);
            }
        }

        /// <summary>
        /// 写Q0.3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ03_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 3,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q03 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);
            }
        }

        /// <summary>
        /// 写Q0.4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ04_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 4,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q04 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);
            }
        }

        /// <summary>
        /// 写Q0.5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ05_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 5,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q05 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);
            }
        }

        /// <summary>
        /// 写Q0.6
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ06_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 6,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q06 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);
            }
        }

        /// <summary>
        /// 写Q0.7
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbQ07_Click(object sender, RoutedEventArgs e)
        {
            if (s7Lib != null && s7Lib.Connected)
            {
                WriteDataParameter writeDataParameter = new WriteDataParameter
                {
                    Area = Area.Q,
                    ParameterType = ParameterType.BIT,
                    WriteCount = 1,
                    ByteAddress = 0,
                    BitAddress = 7,
                    DataType = DataType.BIT,
                    DBNumber = 0,
                    WriteBytes = (Q07 ? [0x01] : [0x00])
                };
                s7Lib.WriteSingle(writeDataParameter);
            }
        }
        #endregion


        #region 写DB区
        private void txtDBW0_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtDBW0.Text == "")
            {
                return;
            }

            if (s7Lib != null && s7Lib.Connected)
            {
                ushort us = ushort.Parse(txtDBW0.Text);
                s7Lib.WriteUShort(1, 0, 0, us);
            }
        }

        private void txtDBW2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtDBW2.Text == "")
            {
                return;
            }

            if (s7Lib != null && s7Lib.Connected)
            {
                short s = short.Parse(txtDBW2.Text);
                s7Lib.WriteShort(1, 2, 0, s);
            }
        }

        private void txtDBD4_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtDBD4.Text == "")
            {
                return;
            }

            if (s7Lib != null && s7Lib.Connected)
            {
                float f = float.Parse(txtDBD4.Text);
                s7Lib.WriteFloat(1, 4, 0, f);
            }
        }

        private void txtStr8_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtStr8.Text.Length > 10)
            {
                MessageBox.Show("字符串长度不能超过10");
                return;
            }

            if (s7Lib != null && s7Lib.Connected)
            {
                s7Lib.WriteString(1, 0x0A, 8, 0, txtStr8.Text);
            }
        }

        #endregion


    }
}
